<template>
  <div class="qmcheck-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :disabled="!currentRef" :icon="Edit" plain type="warning">
          报检单
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Delete"
          plain
          type="danger"
          @click="deleteEquip"
        >
          删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="checks"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="检验单号" prop="checkCode" width="120" />
      <el-table-column label="报检单号" prop="inspectCode" width="120" />
      <el-table-column label="报检类型" width="100">
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.inspectTypeName }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.sourcePk }}
            </span>
          </el-row>
        </template>
      </el-table-column>

      <el-table-column label="存货编码" prop="invcode"  width="260" />
      <el-table-column label="名称型号"  width="260" > 
        <template #default="scope">
          <el-row>
            <span style="font-size: 16px; font-weight: 700; color: #000">
              {{ scope.row.invname }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.invspec }}
            </span>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="报检数" width="130">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600">
            {{ scope.row.qty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="检验数" width="130">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600">
            {{ scope.row.checkQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="合格数" width="130">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #5314bd">
            {{ scope.row.qualifiedQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="不合格" width="130">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #e62b46">
            {{ scope.row.unqualifiedQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="检验完毕" width="100">
        <template #default="scope">
          <el-icon v-if="scope.row.state == 1" color="#30DF80" size="20">
            <Select />
          </el-icon>
          <el-icon v-else color="#DF3730" size="20">
            <SemiSelect />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column label="创建信息" prop="invcode" width="220">
        <template #default="scope">
          <el-row>
            <span>
              {{ scope.row.createUser }}
            </span>
          </el-row>
          <el-row>
            <span>
              {{ scope.row.createTime }}
            </span>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <div style="height: 28px"></div>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="checkbs"
      highlight-current-row
      @current-change="handleCurrentChange2"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="项目编码" prop="itemCode" width="180" />
      <el-table-column label="项目名称" prop="itemName" width="220" />
      <el-table-column label="标准" prop="itemStandard" width="300" />
      <el-table-column label="不合格" width="150">
        <template #default="scope">
          <span style="font-size: 16px; font-weight: 600; color: #5314bd">
            {{ scope.row.unqualifiedQty }}&nbsp;{{ scope.row.measName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" width="350" />
    </el-table>
    <queryCheckDialog
      ref="queryCheckDialogRef"
      @success-callback="queryCallback"
    />
  </div>
</template>

<script setup>
  import { Search, Delete, Select, SemiSelect } from '@element-plus/icons-vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { queryCheckHPageApi, queryQmCheckBSApi } from '@/api/qm/index'
  import queryCheckDialog from './modules/queryCheckDialog.vue'
  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
  })

  const checks = ref()
  const checkbs = ref()
  const queryCheckDialogRef = ref()

  const queryCheckHPage = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await queryCheckHPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        checks.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => queryCheckDialogRef.value.openDialog()

  const queryCallback = (value) => {
    queryForm.value = value
    queryForm.value.ksDate = value.queryDate[0]
    queryForm.value.jsDate = value.queryDate[1]
    queryCheckHPage()
  }

  const handleCurrentChange = async (row) => {
    let params = {
      pkCheckH: row.pkCheckH,
    }
    const { code, data } = await queryQmCheckBSApi(params)
    if (code == 2000) {
      checkbs.value = data
    }
  }

  //#region pagintion 开始
  const layout = ref('prev, pager, next')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => queryCheckHPage())

  onMounted(() => queryCheckHPage())
</script>
